Map Function

A collection in Scala is a data structure which holds a group of objects. Examples of collections include Arrays, Lists, etc. We can apply some transformations to these collections using several methods. One such widely used method offered by Scala is map().

Important points about map() method:
  •     map() is a higher order function.
  •     Every collection object has the map() method.
  •     map() takes some function as a parameter.
  •     map() applies the function to every element of the source collection.
  •     map() returns a new collection of the same type as the source collection.
Example

object Demo {
   def main(args: Array[String]) {
    val num=List(1,2,3,4,5)
    val res=num.map(x => (x* 2))
    println(res)
       }
}


Pass in function
object Demo {
   def main(args: Array[String]) {
    def mul(i :Int) = i* 2
    val num=List(1,2,3,4,5)
    val res=num.map(mul)
    println(res)
   }
}


To find the square of Numbers
object Demo {
    def main(args: Array[String]){
    def square(i:Int):Int={
     i * i
   }
    val num=List(1,2,3,4,5)
    val res = num.map(square)
    println("Squar of a number" + res)
  }
}


To find the square of Numbers
object Demo {
    def main(args: Array[String]){
    val num=List(1,2,3,4,5)
    val res = num.map(x => x * x )
    println("Squar of a number" + res)
  }
}



val list = List("one","two","three")
val lengths = list.map(_.length)


val patterns = list.map({
      case "one" => Some(1)
      case "two" => Some(2)
      case  _ => None
      })


val myMap = Map("a" -> 1, "b" -> 2, "c" -> 3)
val newMyMap = myMap.map{case (key, value) => key -> (value * 2)}


val people = List("adam", "kim", "melissa")
val caps1 = people.map(_.capitalize)
val caps2 = for (f <- people) yield f.capitalize


object Demo {
    def main(args: Array[String]){
    val fruits = List("apple", "banana", "lime", "orange", "raspberry")
    val newfruits=fruits.map{f =>
              if(f.length < 6)f.toUpperCase
    }
    println (newfruits)
  
  }
}
mapValues
When we use map() with a Pair RDD, we get access to both Key & value. There are times we might only be interested in accessing the value(& not key). In those case, we can use mapValues() instead of map().






val myMap = Map("a" -> 1, "b" -> 2, "c" -> 3)
val newMyMap = myMap.mapValues(i => i * 2)


In this example we use mapValues() along with reduceByKey() to calculate average for each subject
val inputrdd = sc.parallelize(Seq(("maths", 50), ("maths", 60), ("english", 65)))
val mapped = inputrdd.mapValues(mark => (mark, 1));

val reduced = mapped.reduceByKey((x, y) => (x._1 + y._1, x._2 + y._2))
val average = reduced.map { x =>
                           val temp = x._2
                           val total = temp._1
                           val count = temp._2
                           (x._1, total / count)
                           }
average.collect
res6: Array[(String, Int)] = Array((english,65), (maths,55))

No comments:

Post a Comment